package com.gdones.wms.bus.warehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.gdones.common.core.dto.MyPageBand;
import com.gdones.common.core.dto.MyPager;
import com.gdones.common.core.dto.MyResult;
import com.gdones.common.core.util.MyPinYinUtil;
import com.gdones.wms.bus.warehouse.dto.WmsWarehouse;
import com.gdones.wms.bus.warehouse.mapper.WmsWarehouseMapper;
import com.gdones.wms.bus.warehouse.service.IWmsWarehouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-05-06
 */
@Service
public class WmsWarehouseServiceImpl extends ServiceImpl<WmsWarehouseMapper, WmsWarehouse> implements IWmsWarehouseService {

    @Override
    public MyPager<WmsWarehouse> pages(WmsWarehouse dto, MyPageBand page) {

        WmsWarehouseMapper warehouseMapper = getBaseMapper();

        IPage<WmsWarehouse> pages = warehouseMapper.pages(PageDTO.of(page.getPage(), page.getSize()), dto);

        // 根据分页的结果封装返回数据
        MyPager<WmsWarehouse> pager = new MyPager<>();
        pager.setTotal(pages.getTotal());
        pager.setPage(page.getPage());
        pager.setPageSize(page.getSize());
        pager.setRows(pages.getRecords());

        return pager;
    }

    @Override
    public List<WmsWarehouse> mylist(WmsWarehouse dto) {

        WmsWarehouseMapper warehouseMapper = getBaseMapper();

        List<WmsWarehouse> list = warehouseMapper.myList(dto);

        return list;
    }

    @Override
    public MyResult add(WmsWarehouse dto) {

        MyResult result = new MyResult();

        String code = "MW-"+ MyPinYinUtil.getPinYinByString(dto.getName())+"-"+new Date().getTime();

        dto.setCode(code);

        boolean save = this.save(dto);
        if(save){
            result.setMsg("添加成功！");
        }else{
            result.setMsg("添加失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult update(WmsWarehouse dto) {

        MyResult result = new MyResult();

        boolean b = updateById(dto);
        if(b){
            result.setMsg("修改成功！");
        }else{
            result.setMsg("修改失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult deletes(String ids) {

        MyResult result = new MyResult();

        String[] split = ids.split(",");
        List<String> idList = Arrays.asList(split);

        boolean b = this.removeBatchByIds(idList);
        if(b){
            result.setMsg("删除成功！");
        }else{
            result.setMsg("删除失败！");
            result.setCode("9999");
        }

        return result;
    }
}
